package com.whp.openapi.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whp.openapi.common.ResponseResult;
import com.whp.openapi.common.ResultTable;
import com.whp.openapi.dao.RechargeMapper;
import com.whp.openapi.entity.Recharge;
import com.whp.openapi.service.RechargeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author whp
 * @date 2021/11/26
 */
@Service
public class RechargeServiceImpl implements RechargeService {
    
    @Autowired
    private RechargeMapper rechargeMapper;

    @Override
    public ResultTable table(Integer page, Integer limit, String name, Integer state) {
        PageHelper.startPage(page, limit);
        List<Recharge> recharges = rechargeMapper.getTable(name, state);
        PageInfo pi = new PageInfo(recharges);
        return new ResultTable(0, "msg", (int)pi.getTotal(), pi.getList());
    }

    @Override
    public ResponseResult delRecharge(Integer[] ids) {
        int i = rechargeMapper.delRecharge(ids);
        if (i > 0) {
            return ResponseResult.getSuccess();
        }
        return ResponseResult.getFail("删除失败");
    }

    @Override
    public ResponseResult addRecharge(Recharge recharge) {
        if (recharge != null) {
            int i = rechargeMapper.insertSelective(recharge);
            if (i > 0) {
                return ResponseResult.getSuccess();
            }
        }
        return ResponseResult.getFail("添加失败");
    }

    @Override
    public ResponseResult updateRecharge(Recharge recharge) {
        if (recharge != null) {
            int i = rechargeMapper.updateByPrimaryKeySelective(recharge);
            if (i > 0) {
                return ResponseResult.getSuccess();
            }
        }
        return ResponseResult.getFail("修改失败");
    }
}
